home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / V15N04.ZIP / WARPCA.ZIP / WCABSRC.ZIP / RUNCMD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  3.4 KB  |  171 lines

  1. // DRunCmdDlg -- Class implementation
  2. #include <assert.h>
  3. #include <cstring.h>
  4. #include <process.h>
  5. #include <classlib\arrays.h>
  6. #include <owl\owlpch.h>
  7. #include <owl\dialog.h>
  8. #include <owl\edit.h>
  9. #include <owl\static.h>
  10. #include <owl\button.h>
  11. #include "resource.h"
  12. #include "filentry.h"
  13. #include "filetool.h"
  14. #include "runcmd.h"
  15.  
  16. #define DID_OK      1
  17. #define DID_CANCEL  2
  18.  
  19. DEFINE_RESPONSE_TABLE1(DRunCmdDlg, TDialog)
  20.     EV_CHILD_NOTIFY(IDC_CMD_LINE, EN_CHANGE, OnCmdLineBoxChange),
  21.     EV_CHILD_NOTIFY(DID_OK, BN_CLICKED, CmOk),
  22. END_RESPONSE_TABLE;
  23.  
  24. void DRunCmdDlg::SetupWindow()
  25. {
  26.     TDialog::SetupWindow();
  27.  
  28.     SetDlgItemText(IDC_CURRENT_DIR, lpszCurrentDir);
  29.  
  30.     SendDlgItemMsg(IDC_CMD_LINE, EM_SETTEXTLIMIT, 1025, 0);
  31.  
  32.     if(lpszInitCmd && strlen(lpszInitCmd) == 0)
  33.         WinEnableWindow(GetDlgItem(DID_OK), 0);
  34.     else
  35.         SetDlgItemText(IDC_CMD_LINE, lpszInitCmd);
  36. }
  37.  
  38. void DRunCmdDlg::OnCmdLineBoxChange()
  39. {
  40.     // Enable or disable OK button based on whether target box is empty.
  41.     int cc = WinQueryWindowTextLength(GetDlgItem(IDC_CMD_LINE));
  42.     WinEnableWindow(GetDlgItem(DID_OK), cc);
  43. }
  44.  
  45. void DRunCmdDlg::CmOk()
  46. {
  47.     int rc;
  48.     int i;
  49.  
  50.     const int MAX_ARGS = 32;
  51.  
  52.     static char szCmdLine[1025];
  53.  
  54.     string strCmdRunIt;
  55.  
  56.     GetDlgItemText(IDC_CMD_LINE, szCmdLine, 1025);
  57.  
  58.     static LPSTR pArgs[MAX_ARGS + 1];
  59.  
  60.     LPSTR p;
  61.     int nArgCount = 0;
  62.     int bFirst = TRUE;
  63.     while(TRUE)
  64.     {
  65.         if(bFirst)
  66.         {
  67.             p = strtok(szCmdLine, " ");
  68.             bFirst = FALSE;
  69.         }
  70.         else
  71.         {
  72.             p = strtok(NULL, " ");
  73.         }
  74.  
  75.         if(p)
  76.         {
  77.             if(strlen(p))
  78.             {
  79.                 if(nArgCount >= MAX_ARGS - 1)
  80.                 {
  81.                     MessageBox("Too many command line arguments!", "Can't Run Command", MB_ICONEXCLAMATION);
  82.                     CloseWindow(FALSE);
  83.                     return;
  84.                 }
  85.                 nArgCount++;
  86.                 pArgs[nArgCount - 1] = p;
  87.             }
  88.         }
  89.         else
  90.             break;  // Nothing more to parse!
  91.     }
  92.  
  93.     if(nArgCount == 0)
  94.     {
  95.         MessageBox("Nothing to run.", "Can't Run Command", MB_ICONEXCLAMATION);
  96.         CloseWindow(FALSE);
  97.         return;
  98.     }
  99.     else
  100.         pArgs[nArgCount] = NULL;  // Append last null to array of pointers.
  101.  
  102.     // Check which kind of .BAT / .EXE / .CMD / .COM file
  103.     strCmdRunIt = pArgs[0];
  104.     strCmdRunIt.to_upper();
  105.  
  106.     string strTemp1;
  107.     string strPgm;
  108.     string strArgs;
  109.  
  110.     int bUseArgs = FALSE;
  111.  
  112.     strTemp1 = ".CMD";
  113.     if(strCmdRunIt.find_first_of(strTemp1) != NPOS)
  114.     {
  115.         // Then it's a .CMD file
  116.         strPgm = "CMD.EXE";
  117.  
  118.         strArgs = "/K";
  119.         for(i = 0; i < nArgCount; i++)
  120.         {
  121.             strArgs += " ";
  122.             strArgs += pArgs[i];
  123.         }
  124.         bUseArgs = TRUE;
  125.     }
  126.     else
  127.     {
  128.         strTemp1 = ".BAT";
  129.         // Then it's a .BAT file
  130.         if(strCmdRunIt.find_first_of(strTemp1) != NPOS)
  131.         {
  132.             strPgm = "COMMAND.COM";
  133.  
  134.             strArgs = "";
  135.             for(i = 0; i < nArgCount; i++)
  136.             {
  137.                 strArgs += " ";
  138.                 strArgs += pArgs[i];
  139.             }
  140.             bUseArgs = TRUE;
  141.         }
  142.         else
  143.         {
  144.             // Treat it as a runnable file on its own--an .EXE or .BAT
  145.             strPgm = pArgs[0];
  146.  
  147.             strArgs = "";
  148.             for(i = 1; i < nArgCount; i++)
  149.             {
  150.                 if(i > 1)
  151.                     strArgs += " ";
  152.                 strArgs += pArgs[i];
  153.             }
  154.             if(i > 1)
  155.                 bUseArgs = TRUE;        }
  156.     }
  157.  
  158.     // To here, run program and report result.
  159.     rc = RunProgramWithArgs((char *)strPgm.c_str(),
  160.                                     bUseArgs ? (char *)strArgs.c_str() : NULL);
  161.     if(!rc)
  162.     {
  163.         MessageBox("There was an error running this program.  Check that path is correct or close some programs to free up memory and try again.", "Can't Run", MB_ICONSTOP);
  164.         CloseWindow(FALSE);
  165.     }
  166.     else
  167.         CloseWindow(TRUE);
  168. }
  169.  
  170.  
  171.